package com.da.lon.wang.xlg.imgupload;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.util.WebViewUtil;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PicDetailActivity extends Activity implements OnClickListener {

    private GridView gridView;
    private String dir;
    private Button sure;
    private String flg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        try {
            MyApplication.getInstance().addActivity(this);
            setContentView(R.layout.xlg_image_detail);
            dir = getIntent().getStringExtra("dir");
            flg = getIntent().getStringExtra("flg");
            initView();
            File pareantFile = new File(dir);
            List<String> imgs = Arrays.asList(pareantFile.list(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String filename) {
                    if (filename.endsWith(".jpg") || filename.endsWith(".png") || filename.endsWith(".jpeg")) {
                        return true;
                    }

                    return false;
                }
            }));

            List<ImageInfo> infos = new ArrayList<ImageInfo>();
            for (int i = 0; i < imgs.size(); i++) {
                ImageInfo info = new ImageInfo();
                info.path = dir + "/" + imgs.get(i);
                if (isImageSelected(info)) {
                    info.isSelected = true;
                }
                infos.add(info);
            }

            gridView.setAdapter(new ImageGridAdapter(this, infos));
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }


    }

    private void initView() {
        ((ImageButton) findViewById(R.id.image)).setVisibility(View.GONE);
        ((RelativeLayout) findViewById(R.id.top_rl)).setBackgroundColor(Color.parseColor("#f0f0f0"));
        TextView title = (TextView) findViewById(R.id.title);
        title.setText("选择照片");
        title.setTextColor(Color.parseColor("#000000"));
        ImageButton back = (ImageButton) findViewById(R.id.back);
        back.setImageResource(R.drawable.xlg_back_grey);
        back.setOnClickListener(this);
        sure = (Button) findViewById(R.id.sure);
        sure.setOnClickListener(this);
        gridView = (GridView) findViewById(R.id.grid);
    }

    private boolean isImageSelected(ImageInfo imageInfo) {
        List<ImageInfo> imageInfos = MyApplication.getInstance().imageInfos;
        for (int i = 0; i < imageInfos.size(); i++) {
            ImageInfo info = imageInfos.get(i);
            if (info.path.equals(imageInfo.path)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sure:
                MyApplication.getInstance().clearActivityList();
                if (!WebViewUtil.isHaveChoose) {
                    Intent intent;
                    if (flg != null) {
                        if ("take".equals(flg)) {
                            if (MyApplication.isMyoption()) {
                                finish();
                            } else {
                                intent = new Intent(PicDetailActivity.this, ChooseImageAvtivity.class);
                                intent.putExtra("type", "replay");
                                startActivity(intent);
                            }
                        } else if ("setting".equals(flg)) {
                            finish();
                        }
                    }
                }
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }

    }

}
